
import 'package:flutter/material.dart';
import 'SmallVideoPage.dart';
import 'UserProfilePage.dart';
import 'message.dart';
import 'small_world.dart';
import 'contacts.dart';
import 'status.dart';

class HomePage extends StatelessWidget {

  final String nickname;
  final String avatarPath; // 新增头像路径参数

  const HomePage({super.key, required this.nickname, required this.avatarPath});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'QQ',
      home: Scaffold(
        appBar: AppBar(
          title: Text(nickname), // 显示昵称
          elevation: 8.0,
          shadowColor: Colors.grey,
          leading: IconButton(
            icon: Container(
              padding: const EdgeInsets.all(1.0),
              child: CircleAvatar(
                backgroundImage: AssetImage(avatarPath), // 使用注册时的头像
              ),
            ),
            onPressed: () {
              // 点击头像跳转到用户个人界面
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => const UserProfilePage()),
              );
            },
          ),
          backgroundColor: Colors.blue,
          actions: [
            PopupMenuButton(
              child: Container(
                margin: const EdgeInsets.all(1.0),
                child: const Icon(
                  Icons.add,
                  color: Colors.black,
                  size: 35.0,
                ),
              ),
              itemBuilder: (context) => [
                PopupMenuItem(
                  value: 1,
                  onTap: () {},
                  child: const Row(
                    children: [
                      Icon(
                        Icons.group_add,
                        color: Colors.black,
                        size: 25.0,
                      ),
                      SizedBox(width: 14.0),
                      Text('创建群聊'),
                    ],
                  ),
                ),
                PopupMenuItem(
                  value: 2,
                  onTap: () {},
                  child: const Row(
                    children: [
                      Icon(
                        Icons.person_add,
                        color: Colors.black,
                        size: 25.0,
                      ),
                      SizedBox(width: 14.0),
                      Text('加好友/群'),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
        body: const MyHomepage(),
      ),
    );
  }
}

class MyHomepage extends StatefulWidget {
  const MyHomepage({super.key});

  @override
  State<MyHomepage> createState() => _MyHomepageState();
}

class _MyHomepageState extends State<MyHomepage> {
  int _currentIndex = 0;  // 当前选中的页面索引

  // 底部导航栏对应的页面
  final List<Widget> _pages = [
    const MessagePage(),
    const SmallWorldPage(),
    const SmallVideoPage(), // 新增小视频页面
    ContactsPage(),
    const StatusPage(),
  ];

  // 底部导航栏的配置
  final List<BottomNavigationBarItem> _items = [
    const BottomNavigationBarItem(
      icon: Icon(Icons.message),
      label: '消息',
    ),
    const BottomNavigationBarItem(
      icon: Icon(Icons.public),
      label: '小世界',
    ),
    const BottomNavigationBarItem(
      icon: Icon(Icons.video_library),
      label: '小视频', // 新增小视频导航栏项
    ),
    const BottomNavigationBarItem(
      icon: Icon(Icons.contacts),
      label: '联系人',
    ),
    const BottomNavigationBarItem(
      icon: Icon(Icons.donut_large),
      label: '动态',
    ),
  ];

  // 点击底部导航栏切换页面
  void _onItemTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  // 构建页面
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_currentIndex],  // 根据当前索引显示对应页面
      bottomNavigationBar: BottomNavigationBar(
        items: _items,
        currentIndex: _currentIndex,  // 当前选中的页面索引
        selectedItemColor: Colors.blue,
        unselectedItemColor: Colors.grey,
        onTap: _onItemTapped,  // 点击切换页面
        type: BottomNavigationBarType.fixed,  // 显示标签文本
      ),
    );
  }
}
